/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires.entites;

import fr.ipst.spot.entites.Organisme;
import fr.ipst.spot.entites.utils.RoleOrganisme;
import fr.ipst.spot.exceptions.SpotExceptionForm;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public class FormOrganisme extends FormAbstractHtmlEntite<Organisme> {

    private String nom;
    private String adresse;
    private String ville;
    private Integer codePostal;
    private RoleOrganisme roleOrganisme;

    /**
     *
     * @param entity
     */
    public FormOrganisme(Organisme entity) {
        super(new FormOrganismeId(entity.getId()), entity);
        this.nom = entity.getNom();
        this.codePostal = entity.getCodePostal();
        this.adresse = entity.getAdresse();
        this.ville = entity.getVille();
        this.roleOrganisme = entity.getRole();
    }

    /**
     *
     * @param request
     */
    public FormOrganisme(HttpServletRequest request) {
        super(new FormOrganismeId(request), request);
        this.nom = this.getNonNullStringFromRequest(request, "nom");
        this.codePostal = this.getIntegerFromRequest(request, "codePostal", -1);
        this.adresse = this.getNonNullStringFromRequest(request, "adresse");
        this.ville = this.getNonNullStringFromRequest(request, "ville");
        this.roleOrganisme = RoleOrganisme.getRoleOrgansimeByCodeSql(this.getIntegerFromRequest(request, "role", -1));
    }

    @Override
    protected void validate(boolean creation) throws SpotExceptionForm {
        boolean erreur = false;
        if (!creation) {
            try {
                this.formId.validate();
            } catch (SpotExceptionForm e) {
                erreur = true;
                this.concatErreurMsg(e);
            }
        }
        try {
            this.isString255CharsNonEmpty(nom, "nom");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isString255CharsNonEmpty(adresse, "adresse");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isString255CharsNonEmpty(ville, "ville");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isCodePostal(codePostal);
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        if (roleOrganisme == null) {
            erreur = true;
            this.concatErreurMsg(new SpotExceptionForm("Role Orgnisme transmis non valide"));
        }
    }

    @Override
    public Organisme getEntityToPersist() throws SpotExceptionForm {
        return this.garnir(new Organisme(), true);
    }

    /**
     *
     * @param entityToFeed
     * @return
     * @throws SpotExceptionForm
     */
    @Override
    protected Organisme garnir(Organisme entityToFeed, boolean creation) throws SpotExceptionForm {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     *
     * @return
     */
    public String getAdresse() {
        return adresse;
    }

    /**
     *
     * @param adresse
     */
    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    /**
     *
     * @return
     */
    public Integer getCodePostal() {
        return codePostal;
    }

    /**
     *
     * @param codePostal
     */
    public void setCodePostal(Integer codePostal) {
        this.codePostal = codePostal;
    }

    /**
     *
     * @return
     */
    public String getNom() {
        return nom;
    }

    /**
     *
     * @param nom
     */
    public void setNom(String nom) {
        this.nom = nom;
    }

    /**
     *
     * @return
     */
    public RoleOrganisme getRoleOrganisme() {
        return roleOrganisme;
    }

    /**
     *
     * @param roleOrganisme
     */
    public void setRoleOrganisme(RoleOrganisme roleOrganisme) {
        this.roleOrganisme = roleOrganisme;
    }

    /**
     *
     * @return
     */
    public String getVille() {
        return ville;
    }

    /**
     *
     * @param ville
     */
    public void setVille(String ville) {
        this.ville = ville;
    }
}
